package com.tycmc.signelectroniccontracts.act.renzheng.qiye;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.ui.camera.CameraActivity;
import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.act.renzheng.ActQiyeRenZheng;
import com.tycmc.signelectroniccontracts.logic.renzheng.qiye.PreFarenxinxi;
import com.tycmc.signelectroniccontracts.tools.base.FraBase;
import com.tycmc.signelectroniccontracts.tools.images.ImageSetting;
import com.tycmc.signelectroniccontracts.tools.images.UrlPath;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.other.TyTypenum;
import com.tycmc.signelectroniccontracts.tools.permissions.PermissionsTool;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;
import com.tycmc.signelectroniccontracts.tools.store.SharePre;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;
import com.tycmc.signelectroniccontracts.tools.times.DataTimer;
import com.tycmc.signelectroniccontracts.ui.PopBottom;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FraFarenxinxi extends FraBase implements PreFarenxinxi.FaRenxinxiInterface {

    private  String filePath;
    private PreFarenxinxi pre;
    private Map upMap;

    public FraFarenxinxi(Map upMap) {
        filePath = System.currentTimeMillis() + "";
        pre = new PreFarenxinxi(this,filePath);
        this.upMap = upMap;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fra_gerenxinxi, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        pre.onResult(requestCode, resultCode, data);
    }

    private void initView() {
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_xingming_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_sex_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_mingzu_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_chushenriqi_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_shenfenzhenghao_state));
        TextShow.setTextNeed((TextView) getView().findViewById(R.id.tv_zhuzhi_state));
        getView().findViewById(R.id.tv_chushenriqi_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataTimer.choseDay(getActivity(), new DataTimer.TimerCallBack() {
                    @Override
                    public void callBack(String time) {
                        ((TextView) getView().findViewById(R.id.tv_chushenriqi)).setText(time);
                    }
                }, ((TextView) getView().findViewById(R.id.tv_chushenriqi)).getText().toString().trim(), "", "");
            }
        });
        getView().findViewById(R.id.tv_sex_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopBottom popBottom = new PopBottom(getActivity(), new PopBottom.OnItemListen() {
                    @Override
                    public void itemOnclick(AdapterView<?> parent, int position, View view) {
                        if (parent.getAdapter().getItem(position) instanceof Map) {
                            Map item = (Map) parent.getAdapter().getItem(position);
                            ((TextView) getView().findViewById(R.id.tv_sex)).setText(item.get(PopBottom.NAME) + "");
                        }
                    }
                });
                List<Map> list = new ArrayList<>();
                Map map = new HashMap();
                map.put(PopBottom.NAME, "男");
                Map map1 = new HashMap();
                map1.put(PopBottom.NAME, "女");
                list.add(map);
                list.add(map1);
                popBottom.addAll(list);
                popBottom.show(v);
            }
        });
        getView().findViewById(R.id.ll_add_before).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new PermissionsTool.PerMissionCallBack() {
                    @Override
                    public void permissIsPass(String[] permission, boolean isPass) {
                        if (isPass) {
                            UrlPath.init();
                            Intent intent = new Intent(getActivity(), CameraActivity.class);
                            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                                    UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_FRONT +filePath+ ".png");
                            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
                            startActivityForResult(intent, TyTypenum.renlianTouxiang);
                        } else {
                            if (permission[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                ToastShow.popupToast("请开启文件存储权限");
                            } else if (permission[0].equals(Manifest.permission.CAMERA)) {
                                ToastShow.popupToast("请开启照相机权限");
                            }
                        }

                    }
                }, ((ActQiyeRenZheng) getActivity()));

            }
        });
        getView().findViewById(R.id.ll_add_end).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new PermissionsTool.PerMissionCallBack() {
                    @Override
                    public void permissIsPass(String[] permission, boolean isPass) {
                        if (isPass) {
                            UrlPath.init();
                            Intent intent = new Intent(getActivity(), CameraActivity.class);
                            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                                    UrlPath.imagePath + IDCardParams.ID_CARD_SIDE_BACK +filePath+ ".png");
                            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
                            startActivityForResult(intent, TyTypenum.renlianBack);
                        } else {
                            if (permission[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                ToastShow.popupToast("请开启文件存储权限");
                            } else if (permission[0].equals(Manifest.permission.CAMERA)) {
                                ToastShow.popupToast("请开启照相机权限");
                            }
                        }

                    }
                }, ((ActQiyeRenZheng) getActivity()));

            }
        });
        getView().findViewById(R.id.tv_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map upMap = getUpMap();
                if (upMap != null) {
                    pre.upMap(upMap);
                }
            }
        });
        ((RadioGroup) getView().findViewById(R.id.rg_farenxinxi)).check(R.id.rb_fadingdaibiaoren);
    }

    @Override
    public void sendPicterBefore(String before) {
        if (new File(before).exists()) {
            getView().findViewById(R.id.ll_add_before).setVisibility(View.GONE);
            getView().findViewById(R.id.iv_before).setVisibility(View.VISIBLE);
            ImageView imageView = (ImageView) getView().findViewById(R.id.iv_before);
            imageView.setTag(before);
            ImageSetting.urlImage(before, imageView);
        } else {
            getView().findViewById(R.id.iv_before).setTag(null);
            getView().findViewById(R.id.ll_add_before).setVisibility(View.VISIBLE);
            getView().findViewById(R.id.iv_before).setVisibility(View.GONE);
        }
    }

    @Override
    public void sendPicterEnd(String end) {
        if (new File(end).exists()) {
            getView().findViewById(R.id.ll_add_end).setVisibility(View.GONE);
            getView().findViewById(R.id.iv_end).setVisibility(View.VISIBLE);
            ImageView imageView = (ImageView) getView().findViewById(R.id.iv_end);
            imageView.setTag(end);
            ImageSetting.urlImage(end, imageView);
        } else {
            getView().findViewById(R.id.iv_end).setTag(null);
            getView().findViewById(R.id.ll_add_end).setVisibility(View.VISIBLE);
            getView().findViewById(R.id.iv_end).setVisibility(View.GONE);
        }
    }

    @Override
    public void initData(String clientName, String sex, String nation, String dateOfBirth, String idcNo, String address) {
        ((TextView) getView().findViewById(R.id.et_xingming)).setText(clientName);
        ((TextView) getView().findViewById(R.id.tv_sex)).setText(sex);
        ((TextView) getView().findViewById(R.id.et_mingzu)).setText(nation);
        ((TextView) getView().findViewById(R.id.tv_chushenriqi)).setText(dateOfBirth);
        ((TextView) getView().findViewById(R.id.et_shenfenzhenghao)).setText(idcNo);
        ((TextView) getView().findViewById(R.id.et_zhuzhi)).setText(address);
    }

    @Override
    public void next(Map upMap) {
        upMap.putAll(this.upMap);
        if (getActivity() instanceof ActQiyeRenZheng) {
            ((ActQiyeRenZheng) getActivity()).showNum(ActQiyeRenZheng.renliaanrenzhen, upMap);
        }
    }

    public Map getUpMap() {
        HashMap map = new HashMap();
        String xingming = ((TextView) getView().findViewById(R.id.et_xingming)).getText().toString().trim();
        String sex = ((TextView) getView().findViewById(R.id.tv_sex)).getText().toString().trim();
        String mingzu = ((TextView) getView().findViewById(R.id.et_mingzu)).getText().toString().trim();
        String chushenriqi = ((TextView) getView().findViewById(R.id.tv_chushenriqi)).getText().toString().trim();
        String shenfenzhenghao = ((TextView) getView().findViewById(R.id.et_shenfenzhenghao)).getText().toString().trim();
        String zhuzhi = ((TextView) getView().findViewById(R.id.et_zhuzhi)).getText().toString().trim();
        if (getView().findViewById(R.id.iv_before).getTag() != null) {
            map.put("before", new File(getView().findViewById(R.id.iv_before).getTag() + ""));
        } else {
            ToastShow.popupToast("请拍照身份证正面");
            return null;
        }
        if (getView().findViewById(R.id.iv_end).getTag() != null) {
            map.put("end", new File(getView().findViewById(R.id.iv_end).getTag() + ""));
        } else {
            ToastShow.popupToast("请拍照身份证背面");
            return null;
        }
        if (TextShow.isEmpty(xingming)) {
            ToastShow.popupToast("请输入姓名");
            return null;
        }
        if (TextShow.isEmpty(sex)) {
            ToastShow.popupToast("请选择性别");
            return null;
        }
        if (TextShow.isEmpty(mingzu)) {
            ToastShow.popupToast("请输入民族");
            return null;
        }
        if (TextShow.isEmpty(chushenriqi)) {
            ToastShow.popupToast("请选择出生日期");
            return null;
        }
        if (TextShow.isEmpty(shenfenzhenghao)) {
            ToastShow.popupToast("请输入身份证号");
            return null;
        }
        if (TextShow.isEmpty(zhuzhi)) {
            ToastShow.popupToast("请输入住址");
            return null;
        }
        RadioGroup group = (RadioGroup) getView().findViewById(R.id.rg_farenxinxi);
        if (group.getCheckedRadioButtonId() == R.id.rb_weituoren) {
            map.put("relationshipID", "1");
            map.put("relationship", "代理人");
        } else {
            map.put("relationshipID", "2");
            map.put("relationship", "法人");
            if (!upMap.get("cpyRepresentive").equals(xingming)) {
                ToastShow.popupToast("法人姓名不正确");
                return null;
            }
            if (!upMap.get("cpyRepresentiveIdcNo").equals(shenfenzhenghao)) {
                ToastShow.popupToast("法人身份证号不正确");
                return null;
            }
        }
        map.put("linkClientName", xingming);
        map.put("sex", sex);
        map.put("nation", mingzu);
        map.put("dateOfBirth", chushenriqi);
        map.put("linkClientIdcNo", shenfenzhenghao);
        map.put("address", zhuzhi);
        map.put("clientArchiveNo", SharePre.getString(SharePreName.clientArchiveNo,""));
        return map;
    }


}
